package com.alogic.xscript.plugins;

import org.apache.commons.lang3.StringUtils;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.XsObjectProperties;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * 桥接上下文
 * 
 * @author yyduan
 * @version 1.6.12.4 [20171029 duanyy] <br>
 * - 支持bridge机制; <br>
 */
public class Bridge extends Segment{
	protected String $id = "$bridge";
	
	public Bridge(String tag, Logiclet p) {
		super(tag, p);
	}

	@Override
	public void configure(Properties p){
		super.configure(p);
		
		$id = PropertiesConstants.getRaw(p,"id",$id);
	}
	
	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		String id = PropertiesConstants.transform(ctx, $id, "");
		
		if (StringUtils.isEmpty(id)){
			super.onExecute(root, current, ctx, watcher);
		}else{
			try {
				ctx.setObject(id, ctx);				
				super.onExecute(root, current, ctx, watcher);
			}finally{
				ctx.removeObject(id);
			}
		}
	}
	
	/**
	 * 向Bridge上下文设置变量
	 * 
	 * @author yyduan
	 *
	 */
	public static class Set extends AbstractLogiclet {
		protected String $id;
		protected String $value;
		protected String $dftValue = "";
		protected boolean ref = false;
		protected boolean raw = false;
		protected String $bridge = "$bridge";
		
		public Set(String tag, Logiclet p) {
			super(tag, p);
		}

		public void configure(Properties p){
			super.configure(p);
			
			$id = PropertiesConstants.getRaw(p,"id","");
			$value = PropertiesConstants.getRaw(p,"value","");
			ref = PropertiesConstants.getBoolean(p,"ref",ref,true);
			raw = PropertiesConstants.getBoolean(p,"raw",raw,true);
			$dftValue = PropertiesConstants.getRaw(p,"dft",$dftValue);
			$bridge = PropertiesConstants.getRaw(p,"bridge",$bridge);
		}

		@Override
		protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
			String bridge = PropertiesConstants.transform(ctx, $bridge, "");
			LogicletContext bridgeContext = ctx.getObject(bridge);
			if (bridgeContext != null){
				String id = PropertiesConstants.transform(ctx, $id, "");
				if (StringUtils.isNotEmpty(id)){
					XsObjectProperties p = new XsObjectProperties(current,ctx);
					if (raw){
						if (ref){
							String v = PropertiesConstants.getRaw(p, $value, "");
							if (StringUtils.isEmpty(v)){
								bridgeContext.SetValue(id, $dftValue);
							}else{
								bridgeContext.SetValue(id, v);
							}
						}else{
							bridgeContext.SetValue(id, $value);
						}
					}else{
						String v = PropertiesConstants.transform(p,$value,"");
						String dft = PropertiesConstants.transform(p,$dftValue,"");
						if (StringUtils.isEmpty(v)){
							v = dft;
						}
						if (ref){
							v = PropertiesConstants.getString(p,v,dft,false);
						}
						
						bridgeContext.SetValue(id, v);
					}
				}				
			}
		}
	}		
}
